defmodule Giom.Accounting.Stock do
  use Ecto.Schema
  import Ecto.Changeset

  @primary_key {:code, :string, autogenerate: false}
  @derive {Phoenix.Param, key: :code}
  schema "stocks" do
	field :name, :string
	
	field :group_code,  :string
	field :group_code2, :string
	field :group_code3, :string
	field :group_code4, :string
	field :group_code5, :string
	
	field :barcode,  :string ,size: 35
	field :barcode2, :string, size: 35
	field :barcode3, :string, size: 35
	field :barcode4, :string, size: 35
	
	field :p_price,  :decimal
	field :p_price2, :decimal
	field :p_price3, :decimal
	field :p_price4, :decimal
	
	field :s_price,  :decimal
	field :s_price2, :decimal
	field :s_price3, :decimal
	field :s_price4, :decimal
	
	field :m_unit,  :string,size: 2
	field :m_unit2, :string,size: 2
	field :m_unit3, :string,size: 2
	field :m_unit4, :string,size: 2
	
	field :pack_type,   :string,size: 2
	field :pack_type2,  :string,size: 2
	field :pack_type3,  :string,size: 2

	field :pack_rate, :decimal 
	field :pack_rate2, :decimal 
	field :pack_rate3, :decimal 
	
	field :unit_weight, :decimal
	field :type, :string,  size: 1
	field :acc_code, :string,  size: 20
	
	field :prj_code, :string,  size: 10
	field :prj_code2, :string, size: 10
	field :prj_code3, :string, size: 10
	field :prj_code4, :string, size: 10
	field :prj_code5, :string, size: 10
    
    field :lock, :string,  size: 1
    
    field :create_date, :utc_datetime
	field :edit_date, :utc_datetime

	field :creator, :string
	field :editor, :string

    timestamps()
  end

  @doc false
  def changeset(stock, attrs) do
    stock
    |> cast(attrs, [:code,:name])
    |> validate_required([:code,:name])
    |> unique_constraint(:code, name: :stocks_pkey)
  end
end


# 1 pack_type  = pack_rate  * m_unit  e.g 1 PK = 24  AD
# 1 pack_type2 = pack_rate2 * m_unit  e.g 1 KL = 100 AD

# type sahası normal, demirbaş, hizmet vs
# acc_code muhasebe kodu
